package spring.framework.core.io;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

import java.net.MalformedURLException;

/**
 * @author fun.pengzh
 * @class spring.framework.core.io.DefaultResourceLoader
 * @desc
 * @since 2021-09-30
 */
public class DefaultResourceLoader implements ResourceLoader {
    @Override
    public Resource getResource(String location) {
        Assert.notNull(location, "parameter location can not be null");
        Resource resource = null;
        if (StrUtil.startWith(location, CLASSPATH_PREFIX)) {
            resource = new ClassPathResource(location.substring(CLASSPATH_PREFIX.length()));
        } else {
            try {
                resource = new UrlResource(location);
            } catch (MalformedURLException e) {
                resource = new FileSystemResource(location);
            }
        }
        return resource;
    }
}
